<template>
	<view class="group-buying-page">
		<view class="group-buying-container">
			<!-- 宣传横幅 -->
			<view class="banner">
				<text>邀请好友拼团 享超值优惠</text>
			</view>
			<!-- 我的团购 -->
			<text class="section-title">我的团购</text>
			<view class="my-groups-container" v-if="myGroups.length > 0">
				<view class="group-list">
					<view class="group-item" v-for="item in myGroups" :key="item.id" @click="goGroupDetail(item.id)">
						<image class="group-image" :src="item.image" mode="aspectFill"></image>
						<view class="group-info">
							<text class="group-name">{{item.name}}</text>
							<view class="group-price">
								<text class="current-price">¥{{item.currentPrice}}</text>
								<text class="original-price">¥{{item.originalPrice}}</text>
							</view>
							<view class="group-status">
								<text
									:class="['status-tag', 'status-' + item.status]">{{getStatusText(item.status)}}</text>
								<text class="status-time">{{item.statusTime}}</text>
							</view>
							<button class="group-button" @click.stop="checkGroupStatus(item.id)">查看详情</button>
						</view>
					</view>
				</view>
			</view>
			<view class="empty-groups" v-else>
				<view class="empty-icon">🛒</view>
				<text class="empty-text">您还没有参与任何团购</text>
				<button class="empty-button" @click="scrollToHotGroups">去看看热门拼团</button>
			</view>
			<!-- 倒计时 -->
			<view class="countdown-container">
				<text class="countdown-title">本场拼团结束还剩</text>
				<view class="countdown">
					<view class="countdown-box">{{countdown.hours}}</view>
					<text class="countdown-separator">:</text>
					<view class="countdown-box">{{countdown.minutes}}</view>
					<text class="countdown-separator">:</text>
					<view class="countdown-box">{{countdown.seconds}}</view>
				</view>
			</view>

			<!-- 热门拼团 -->
			<text class="section-title">热门拼团</text>
			<view class="group-list">
				<view class="group-item" v-for="item in hotGroups" :key="item.id" @click="goGroupDetail(item.id)">
					<image class="group-image" :src="item.image" mode="aspectFill"></image>
					<view class="group-info">
						<text class="group-name">{{item.name}}</text>
						<view class="group-price">
							<text class="current-price">¥{{item.currentPrice}}</text>
							<text class="original-price">¥{{item.originalPrice}}</text>
						</view>
						<view class="group-progress">
							<text>已拼{{item.joined}}件</text>
							<view class="progress-bar">
								<view class="progress-fill" :style="{width: item.progress + '%'}"></view>
							</view>
							<text>还差{{item.remaining}}人</text>
						</view>
						<button class="group-button" @click.stop="joinGroup(item.id)">去拼团</button>
					</view>
				</view>
			</view>

			<!-- 即将结束 -->
			<text class="section-title">即将结束</text>
			<view class="group-list">
				<view class="group-item" v-for="item in endingGroups" :key="item.id" @click="goGroupDetail(item.id)">
					<image class="group-image" :src="item.image" mode="aspectFill"></image>
					<view class="group-info">
						<text class="group-name">{{item.name}}</text>
						<view class="group-price">
							<text class="current-price">¥{{item.currentPrice}}</text>
							<text class="original-price">¥{{item.originalPrice}}</text>
						</view>
						<view class="group-progress">
							<text>已拼{{item.joined}}件</text>
							<view class="progress-bar">
								<view class="progress-fill" :style="{width: item.progress + '%'}"></view>
							</view>
							<text>还差{{item.remaining}}人</text>
						</view>
						<button class="group-button" @click.stop="joinGroup(item.id)">去拼团</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		onUnmounted
	} from 'vue'

	// 倒计时数据
	const countdown = ref({
		hours: '01',
		minutes: '23',
		seconds: '45'
	})

	// 热门拼团数据
	const hotGroups = ref([{
			id: 1,
			name: '北欧风格简约陶瓷花瓶摆件',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '39.9',
			originalPrice: '59.9',
			joined: 2,
			remaining: 1,
			progress: 40
		},
		{
			id: 2,
			name: '多功能厨房收纳置物架',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '29.9',
			originalPrice: '49.9',
			joined: 5,
			remaining: 2,
			progress: 70
		},
		{
			id: 3,
			name: '便携式旅行化妆包',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '19.9',
			originalPrice: '39.9',
			joined: 8,
			remaining: 1,
			progress: 80
		},
		{
			id: 4,
			name: '创意桌面小台灯',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '49.9',
			originalPrice: '79.9',
			joined: 3,
			remaining: 2,
			progress: 30
		}
	])

	// 即将结束拼团数据
	const endingGroups = ref([{
			id: 5,
			name: '日式简约餐具套装',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '59.9',
			originalPrice: '99.9',
			joined: 7,
			remaining: 1,
			progress: 90
		},
		{
			id: 6,
			name: '多功能便携充电宝',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '69.9',
			originalPrice: '109.9',
			joined: 4,
			remaining: 2,
			progress: 60
		}
	])

	// 我的团购数据
	const myGroups = ref([{
			id: 7,
			name: '智能温控保温杯',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '89.9',
			originalPrice: '129.9',
			status: 'processing',
			statusTime: '剩余12小时23分',
		},
		{
			id: 8,
			name: '无线蓝牙耳机',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '129.9',
			originalPrice: '199.9',
			status: 'success',
			statusTime: '已成团',
		},
		{
			id: 9,
			name: '多功能料理机',
			image: 'https://via.placeholder.com/300x200?text=商品图片',
			currentPrice: '199.9',
			originalPrice: '299.9',
			status: 'failed',
			statusTime: '未达成',
		}
	])

	// 获取团购状态文本
	const getStatusText = (status) => {
		switch (status) {
			case 'processing':
				return '拼团中'
			case 'success':
				return '拼团成功'
			case 'failed':
				return '拼团失败'
			default:
				return '未知状态'
		}
	}

	// 查看团购状态
	const checkGroupStatus = (id) => {
		uni.navigateTo({
			url: `/pages/group/status?id=${id}`
		})
	}

	// 滚动到热门拼团
	const scrollToHotGroups = () => {
		uni.pageScrollTo({
			selector: '.section-title',
			duration: 300
		})
	}

	let timer = null

	// 倒计时逻辑
	const startCountdown = () => {
		let hours = 1
		let minutes = 23
		let seconds = 45

		timer = setInterval(() => {
			seconds--

			if (seconds < 0) {
				seconds = 59
				minutes--
			}

			if (minutes < 0) {
				minutes = 59
				hours--
			}

			if (hours < 0) {
				clearInterval(timer)
				return
			}

			countdown.value = {
				hours: hours.toString().padStart(2, '0'),
				minutes: minutes.toString().padStart(2, '0'),
				seconds: seconds.toString().padStart(2, '0')
			}
		}, 1000)
	}

	// 返回上一页
	const goBack = () => {
		uni.navigateBack()
	}

	// 跳转拼团详情
	const goGroupDetail = (id) => {
		uni.navigateTo({
			url: `/pages/group/detail?id=${id}`
		})
	}

	// 参加拼团
	const joinGroup = (id) => {
		uni.navigateTo({
			url: `/pages/group/join?id=${id}`
		})
	}

	onMounted(() => {
		startCountdown()
	})

	onUnmounted(() => {
		if (timer) {
			clearInterval(timer)
		}
	})
</script>

<style lang="scss" scoped>
	.group-buying-page {
		background-color: #f8f8f8;
		min-height: 100vh;
		padding-bottom: 20px;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		padding: 10px 15px;
		background-color: #fff;
		border-bottom: 1px solid #eee;

		.title {
			flex: 1;
			text-align: center;
			font-size: 16px;
			font-weight: bold;
		}
	}

	.group-buying-container {
		padding: 15px;
	}

	.banner {
		width: 100%;
		height: 150px;
		background-color: #ff6b81;
		border-radius: 12px;
		display: flex;
		align-items: center;
		justify-content: center;
		color: white;
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 15px;
		background-image: linear-gradient(to right, #ff6b81, #ff8e9e);
	}

	.countdown-container {
		background-color: #fff;
		border-radius: 12px;
		padding: 15px;
		margin-bottom: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.countdown-title {
			font-size: 16px;
			font-weight: 600;
			color: #333;
		}

		.countdown {
			display: flex;
			align-items: center;

			.countdown-box {
				background-color: #333;
				color: white;
				border-radius: 4px;
				padding: 2px 4px;
				margin: 0 2px;
				font-size: 12px;
				font-weight: 600;
				min-width: 20px;
				text-align: center;
			}

			.countdown-separator {
				color: #333;
				margin: 0 2px;
				font-weight: 600;
			}
		}
	}

	.section-title {
		font-size: 18px;
		font-weight: 600;
		margin: 15px 0 10px;
		color: #333;
		display: block;
	}

	.group-list {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 12px;
	}

	.group-item {
		background-color: #fff;
		border-radius: 12px;
		overflow: hidden;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

		.group-image {
			width: 100%;
			height: 120px;
			background-color: #eee;
		}

		.group-info {
			padding: 10px;

			.group-name {
				font-size: 14px;
				font-weight: 500;
				margin-bottom: 5px;
				color: #333;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				overflow: hidden;
				min-height: 40px;
			}

			.group-price {
				display: flex;
				align-items: baseline;

				.current-price {
					font-size: 16px;
					font-weight: 600;
					color: #ff4757;
				}

				.original-price {
					font-size: 12px;
					color: #999;
					text-decoration: line-through;
					margin-left: 5px;
				}
			}

			.group-progress {
				display: flex;
				align-items: center;
				margin-top: 5px;
				font-size: 12px;
				color: #666;

				.progress-bar {
					flex: 1;
					height: 4px;
					background-color: #eee;
					border-radius: 2px;
					margin: 0 5px;
					overflow: hidden;

					.progress-fill {
						height: 100%;
						background-color: #ff4757;
						border-radius: 2px;
					}
				}
			}

			.group-button {
				background-color: #ff4757;
				color: white;
				border: none;
				border-radius: 15px;
				padding: 5px 10px;
				font-size: 12px;
				margin-top: 8px;
				width: 100%;
			}

			.group-status {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 5px;
				font-size: 12px;

				.status-tag {
					padding: 2px 6px;
					border-radius: 10px;
					font-size: 10px;
					color: white;
				}

				.status-processing {
					background-color: #2196F3;
				}

				.status-success {
					background-color: #4CAF50;
				}

				.status-failed {
					background-color: #9E9E9E;
				}

				.status-time {
					color: #666;
					font-size: 10px;
				}
			}
		}
	}

	.empty-groups {
		background-color: #fff;
		border-radius: 12px;
		padding: 30px 15px;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 10px;

		.empty-icon {
			font-size: 40px;
			margin-bottom: 10px;
			color: #ddd;
		}

		.empty-text {
			color: #999;
			font-size: 14px;
			margin-bottom: 15px;
		}

		.empty-button {
			background-color: #ff4757;
			color: white;
			border: none;
			border-radius: 20px;
			padding: 8px 20px;
			font-size: 14px;
		}
	}
</style>